package com.aw.guiguzi.util

import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import java.util.Locale
import java.util.regex.Pattern

object StrUtil {
    /**
     * 匹配指定关键字
     * @param color
     * @param text
     * @param keyword
     * @return
     */
    fun matcherSearchText(color: Int, text: String, keyword: String): SpannableString {
        val string = text.lowercase(Locale.getDefault())
        val key = keyword.lowercase(Locale.getDefault())
        val pattern = Pattern.compile(key)
        val matcher = pattern.matcher(string)
        val ss = SpannableString(text)
        while (matcher.find()) {
            val start = matcher.start()
            val end = matcher.end()
            ss.setSpan(
                ForegroundColorSpan(color), start, end,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }
        return ss
    }
}